﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using B10_Ex02_Elad_034695114_Meyron_026607788;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Infrastructure.ObjectModel;
using Infrastructure.Managers;
using B10_Ex02_EladLebovitch_034695114.Interfaces;

namespace B10_Ex02_Elad_034695114_Meyron_026607788.ObjectModel
{
    public delegate void BulletCollided(object sender, BulletCollidedEventArgs e);

    public class PlayerBullet : Sprite, ICollidable2D, IPlayerObjects, IHarmful
    {
        public event BulletCollided BulletCollidedWithEnemyEvent;
        private const int k_BulletSpeed = -190;
        private const string k_AssetName = @"Sprites\Bullet";

        public PlayerBullet(Game i_game, Vector2 i_StartPos)
            : base(k_AssetName, i_game)
        {
            // Set the starting position
            this.PositionOfOrigin = i_StartPos;

            // Player's bullet is red
            this.TintColor = Color.Red;

            // Set the speed
            this.Velocity = new Vector2(0, k_BulletSpeed);

            // Add to game components
            i_game.Components.Add(this);
            ((ICollisionsManager)i_game.Services.GetService(typeof(ICollisionsManager))).AddObjectToMonitor(this);
        }

        /// <summary>
        /// Updates
        /// </summary>
        public override void Update(GameTime gameTime)
        {
            // Check if the bullet went out of bounds
            if (this.PositionOfOrigin.Y + this.Height < 0)
            {
                this.Enabled = false;
            }

            base.Update(gameTime);
        }

        public override void Collided(ICollidable i_Collidable)
        {
            // If it's an enemy bullet
            if (i_Collidable is IHaveScore)
            {
                // Check if someone wants to know
                if (BulletCollidedWithEnemyEvent != null)
                {
                    BulletCollidedWithEnemyEvent(this, new BulletCollidedEventArgs(i_Collidable as IHaveScore));
                }
            }

            // On any collision 
            this.Enabled = false;
            this.Visible = false;
        }
    }

    /// <summary>
    /// Event args for collision
    /// </summary>
    public class BulletCollidedEventArgs : EventArgs
    {
        public BulletCollidedEventArgs(IHaveScore i_EnemyCollided)
        {
            EnemyDestroyed = i_EnemyCollided;
        }

        public IHaveScore EnemyDestroyed
        { 
            get; 
            set;
        }
    }
}
